package com.ada.spark.mysql

import org.apache.spark.{SparkConf, SparkContext}

object MysqlInsertTest {
    def main(args: Array[String]) {
        val sparkConf = new SparkConf().setMaster("local[2]").setAppName("MysqlInsertTest")
        val sc = new SparkContext(sparkConf)
        val data = sc.parallelize(List("Frank", "Kate", "Jack"))

        data.foreachPartition(insertData)
    }

    def insertData(iterator: Iterator[String]): Unit = {
        Class.forName("com.mysql.jdbc.Driver").newInstance()
        val conn = java.sql.DriverManager.getConnection("jdbc:mysql://hadoop121:3306/rddoperator", "root", "888888")
        iterator.foreach(data => {
            val ps = conn.prepareStatement("insert into rddtable(name) values (?)")
            ps.setString(1, data)
            ps.executeUpdate()
        })
    }
}
